<?php

namespace app\api\controller;
use \app\api\model\Student as StudentModel;
use think\facade\Filesystem;
class Student
{
    public $imageData;
    public function __construct()
    {
        //判断token是否有效，通过token读取学生的所有信息
        $token=input('get.token');
        if (empty($token)||$token=='-1')output_error('token');
        $this->studentData=StudentModel::where('student_token','=',$token)->field();

        if (empty($this->studentData))output_error('token无效，请重新登陆');
    }

    //注册功能
    public function getData()
    {
        //输出学生的所有信息，去除“student_password”字段
        unset($this->studentData);
    }

    public function modifyName()
    {
        //修改学生姓名，学生姓名可重复。
        $name=input('get.name');
        if (empty($name)){
            output_error('信息填写不完整，请填写完整');
        }
        StudentModel::where('student_id','=',$this->studentData['student_id'])->updata(array('student_name'=>$name));
    }

    public function modifyAvatar()
    {
        //第一步：上传文件拿到文件名
        $file = request()->file('image');
        $name = Filesystem::disk('avatar')->putFile('', $file, 'uniqid');

        //第二步：删除原有文件
        if ($this->imageData['student_avatar'] != 'default.png') {
            $filename = app()->getRootPath() . 'public\\upload\\images\\avatar\\' . $this->imageData['student_avatar'];
            unlink($filename);
        }
        //第三步：更新数据表
        StudentModel::where('student_id', '=', $this->imageData)->update(array('student_avatar' => $name));
    }

}
